//
//  VisitorView.swift
//  HMWeibo
//
//  Created by HuaMou.Chen on 15/11/27.
//  Copyright © 2015年 HuaMou.Chen. All rights reserved.
//

import UIKit


class VisitorView: UIView {
    

    // MARK: - 设置视图显示信息
    /// 设置视图显示信息，默认是首页，所以图片名字是可选的，首页就不用设置图片，传个nil
    ///
    /// - parameter imageName: imageName
    /// - parameter title:     title
    func setupInfo(imageName: String?, title: String) {
       messageLabel.text = title
        if imageName != nil {
            iconImageView.image = UIImage(named: imageName!)
            homeImageView.hidden = true
            sendSubviewToBack(maskImageView)
        } else {
            starAnima()
        }
    }
    
    private func starAnima() {
        let anima = CABasicAnimation()
        anima.keyPath = "transform.rotation"
        anima.duration = 20
        anima.repeatCount = MAXFLOAT
        anima.toValue = 2 * M_PI
        // 这样能让动画一直循环执行，视图销毁的时候，动画也会跟着销毁
        anima.removedOnCompletion = false
        iconImageView.layer.addAnimation(anima, forKey: nil)
    }
    
    /// 通过纯代码创建的时候调用这个方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    /// 通过storyb 、 XIB 创建的时候调用这个方法
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //fatalError("init(coder:) has not been implemented")
        setupUI()
    }
    
   // MARK: - 设置界面
    private func setupUI() {
        // 添加控件
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
//        iconImageView.translatesAutoresizingMaskIntoConstraints = false
//        homeImageView.translatesAutoresizingMaskIntoConstraints = false
        // 因为都是添加到view上面的，所以可以遍历子控件来设置这个属性，就不用每个控件都写一遍
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置布局，手写代码布局的时候，通常 参考一个控件来约束，这样约束变化的时候，就能一起变化
        // 1.图标的布局
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        // 2.小房子的布局
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterY, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        // 3.消息文本
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconImageView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 4.注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 5.登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 6.遮罩视图
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskImageView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(registerBtnHeight)-[registerBtn]", options: [], metrics: ["registerBtnHeight" : -35], views: ["mask" : maskImageView, "registerBtn" : registerButton]))
        
        //背景颜色,灰度图即 R = G = B, 能使用颜色设置就不要使用图片设置，这样能提高程序的性能
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
 /// 如果使用image来设置imageView，那么imageView的大小就是image的大小
    /// 图标
     private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
     private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
     /// 小房子
     private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息文本
     private lazy var messageLabel: UILabel = UILabel(message: "关注一些人，常来看看有什么惊喜", fontSize: 14, color: UIColor.darkGrayColor())
    /// 注册按钮
      lazy var registerButton: UIButton = UIButton(title: "注册", fontSize: 14, color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    /// 登录按钮
      lazy var loginButton: UIButton = UIButton(title: "登录", fontSize: 14, color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")

    
}
